<?php

namespace app\common\model\xiluhouse;

use think\Model;

class HouseView extends Model
{

    // 表名
    protected $name = 'xiluhouse_house_view';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    // 获取该用户对该房源的户型收藏数量
    public function getCollectNumAttr($value, $data)
    {
        $user_id = $data['user_id'];
        $house_id = $data['house_id'];
        // 获取该楼盘所有户型id
        $layout_ids = HouseLayout::where('status', 'normal')
            ->where('house_id', $house_id)
            ->column('id');
        $collect_num = HouseCollect::where('user_id', $user_id)
            ->where('type', 2)
            ->where('house_layout_id', 'in', $layout_ids)
            ->count() ?: 0;
        return $collect_num;
    }

    public function house()
    {
        return $this->belongsTo('House', 'house_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
